package com.kern.academic.service.impl;

import com.kern.academic.dto.MenuDTO;
import com.kern.academic.entity.XMenu;
import com.kern.academic.repository.IMenuRepository;
import com.kern.academic.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by caoshi at 8:00 2021-07-01
 */
@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private IMenuRepository menuRepository;

    @Override
    public List<MenuDTO> listAll() {
        List<MenuDTO> dtoList = new ArrayList<>();
        // 所有一级菜单
        List<XMenu> rootMenus = menuRepository.findRootMenus();

        for (XMenu rm : rootMenus) {
            MenuDTO dto = new MenuDTO();
            dto.setId(rm.getId());
            dto.setText(rm.getText());
            dto.setPath(rm.getPath());
            dto.setSeq(rm.getSeq());
            List<MenuDTO> children = dto.getChildren();
            List<XMenu> childs = menuRepository.findByPid(rm.getId());

            for (XMenu child : childs) {
                MenuDTO cdto = new MenuDTO();
                cdto.setId(child.getId());
                cdto.setText(child.getText());
                cdto.setPath(child.getPath());
                cdto.setSeq(child.getSeq());
                children.add(cdto);
            }
            dtoList.add(dto);
        }

        return dtoList;
    }
}
